﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyLambda
{
    public class LinqShow
    {
        private List<Student> GetStudentList()
        {
            List<Student> studentList = new List<Student>() {
                new Student()
                {
                    Id=1,
                    Name="Albert",
                    ClassId=101,
                    Age=33
                },
                new Student()
                {
                    Id=2,
                    Name="",
                    ClassId=101,
                    Age=28
                },
                 new Student()
                {
                    Id=3,
                    Name="",
                    ClassId=101,
                    Age=2258
                },
                  new Student()
                {
                    Id=4,
                    Name="",
                    ClassId=101,
                    Age=26
                }
            };

            return studentList;

        }

        public void Show()
        {
            List<Student> studentList = this.GetStudentList();

            List<Student> studentListLessthan30 = new List<Student>();
            foreach (var item in studentList)
            {
                if (item.Age < 30)
                    studentListLessthan30.Add(item);
            }

            {
                Console.WriteLine("****************************************");
                var list = studentList.Where<Student>(s => s.Age < 30);

                foreach (var item in list)
                    Console.WriteLine("Name {0} Age {1}", item.Name, item.Age);
            }

            {
                Console.WriteLine("****************************************");
                //ExtendShow.AlbertWhere<Student>(studentList, s => s.Age < 30);  与 ExtendShow.ToInt(ivalue); 一个道理
                var list = studentList.AlbertWhere<Student>(s => s.Age < 30);

                foreach (var item in list)
                {
                    Console.WriteLine("Name {0} Age {1}", item.Name, item.Age);
                    Console.WriteLine($"Name {item.Name} Age {item.Age} ");
                }
            }

            {
                Console.WriteLine("****************************************");
                //IEnumerable<Student>  var
                var list = from s in studentList
                           where s.Age < 30
                           select s;

                foreach (var item in list)
                {
                    Console.WriteLine("Name {0} Age {1} ", item.Name, item.Age);
                }
            }

            {
                Console.WriteLine("****************************************");
                var list = studentList.Where<Student>(s => s.Age < 30)
                    .Select(s => new
                    {
                        IdName = s.Id + s.Name,
                        ClassName = s.ClassId == 2 ? "高级班" : "其他班"
                    }
                    );

                foreach (var item in list)
                    Console.WriteLine("IdName {0} ClassName {1}", item.IdName, item.ClassName);
            }

            {
                Console.WriteLine("****************************************");
                var list = studentList.Where<Student>(s => s.Age < 30)
                    .Select(s => new
                    {
                        Id = s.Id,
                        ClassId = s.ClassId,
                        IdName = s.Id + s.Name,
                        ClassName = s.ClassId == 2 ? "高级班" : "其他班"
                    }
                    ).OrderBy(s => s.Id)
                    .OrderByDescending(s => s.ClassId);

                foreach (var item in list)
                    Console.WriteLine("IdName {0} ClassName {1}", item.IdName, item.ClassName);
            }

        }
    }
}
